package cn.tedu.csmall.product.config;

import cn.tedu.csmall.product.interceptor.InsertUpdateTimeInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @Configuration 这个注解标志这个是配置类
 * MyBatis的配置类
 * 作用：将路径cn.tedu.csmall.product.mapper下的各个接口都配置成mapping接口
 * @author stephen
 * @version 0.0.1
 *
 */
@ComponentScan("com.baidu")
@Slf4j
@Configuration
@MapperScan("cn.tedu.csmall.product.mapper")
public class MybatisConfiguration {

    public MybatisConfiguration(){
        log.info("加载配置类：MybatisConfiguration");
    }

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @PostConstruct
    public void addInterceptor(){
        InsertUpdateTimeInterceptor interceptor = new InsertUpdateTimeInterceptor();
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList){
            sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
        }
    }
}
